package v1

import (
	"apibe/single/src/v1/router"
	"apibe/single/src/v1/share/config"
	"apibe/single/src/v1/share/middler"
	"github.com/gin-gonic/gin"
)

func Init() {
	e := gin.Default()
	gin.SetMode(config.Config.Mode)
	all(e) // 端口注册
	err := e.Run(config.Config.Net.Port)
	if err != nil {
		panic(err.Error())
	}
}

const v1 = "v1"

func all(e *gin.Engine) {
	e.Use(middler.CrossMiddleware)
	group := e.Group(v1)
	// design 接口
	{
		design := group.Group("/design")
		design.POST("/process/:api_name", middler.CrossMiddleware, middler.JWTMiddleware, router.Design) // 转换接口
		design.POST("/upsert", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.DesignUpsert)
		design.POST("/findMany", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.DesignFindMany)
	}
	// task 接口
	{
		task := group.Group("/task")
		task.POST("/upsert", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.TaskUpsert)
		task.POST("/findMany", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.TaskFindMany)
	}
	// user 接口
	{
		user := group.Group("/user")
		user.POST("/register", middler.ApiMiddleware, middler.CryptMiddleware, router.UserRegister)
		user.POST("/login", middler.ApiMiddleware, middler.CryptMiddleware, router.UserLogin)
		user.POST("/update", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.UserUpdate)
		user.POST("/findMany", middler.ApiMiddleware, middler.CryptMiddleware, middler.JWTMiddleware, router.UserFindMany)
	}
	// api接口
	{
		api := group.Group("/api")
		api.GET("/getWatch/:api_name", middler.ApiMiddleware, router.ApiGetWatch)
		api.POST("/selector/:action/:selector", middler.ApiMiddleware, router.ApiSelector)
		api.POST("/:work/:action/:id", middler.ApiMiddleware, middler.JWTMiddleware, middler.CryptMiddleware, router.ApiChangeStatus)
		api.POST("/cache_response/:action/:id", middler.ApiMiddleware, middler.CryptMiddleware, router.ApiCacheResponse)
		api.POST("/postman/export", middler.JWTMiddleware, middler.CryptMiddleware, router.ApiExportPostman)
	}
	// tourist 数据库直接操作接口
	{
		tourist := group.Group("/tourist", middler.WhiteMiddleware)
		mongo := tourist.Group("/mongo", middler.ApiMiddleware, middler.CryptMiddleware)
		mongo.PUT("/create_one", router.MongoCreateOne)
		mongo.PUT("/create_many", router.MongoCreateMany)
		mongo.GET("/query_one", router.MongoQueryOne)
		mongo.GET("/query_many", router.MongoQueryMany)
		mongo.POST("/update_one", router.MongoUpdateOne)
		mongo.POST("/update_many", router.MongoUpdateMany)
		mongo.DELETE("/remove_one", router.MongoRemoveOne)
		mongo.DELETE("/remove_many", router.MongoRemoveMany)
		redis := tourist.Group("/redis", middler.ApiMiddleware, middler.CryptMiddleware)
		redis.GET("/get_one", router.RedisGetOne)
		redis.GET("/get_many", router.RedisGetMany)
		redis.PUT("/set_one", router.RedisSetOne)
		redis.PUT("/set_many", router.RedisSetMany)
		redis.DELETE("/del_one", router.RedisRemoveOne)
		redis.DELETE("/del_many", router.RedisRemoveMany)
		mysql := tourist.Group("/mysql", middler.ApiMiddleware, middler.CryptMiddleware)
		mysql.GET("/select_one", router.MysqlSelectOne)
		mysql.GET("/select_many", router.MysqlSelectMany)
		mysql.POST("/exec_one", router.MysqlExecOne)
		mysql.POST("/exec_many", router.MysqlExecMany)
	}
}
